package com.example.tossednfound;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SearchActivity extends Activity{

	public static ArrayList<Item> searchedItems;

	TextView entry_title, entry_subtitle;
	EditText entry_itemName, entry_address, entry_country;
	Button entry_searchButton, entry_cancelButton;
	Spinner spinner_category;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_screen);
		
		entry_title = (TextView) findViewById(R.id.searchScreen_Title);
		entry_subtitle = (TextView) findViewById(R.id.searchScreen_subtitle);
		
		entry_itemName = (EditText) findViewById(R.id.searchScreen_Name);
		entry_address = (EditText) findViewById(R.id.searchScreen_Address);
		entry_country = (EditText) findViewById(R.id.searchScreen_Country);
		
		entry_searchButton = (Button) findViewById(R.id.searchScreen_searchButton);
		entry_cancelButton = (Button) findViewById(R.id.searchScreen_cancelButton);
		
		spinner_category = (Spinner) findViewById(R.id.searchScreen_categoryDropDown);

		addCategoriesToSpinner();
		
		findViewById(R.id.searchScreen_searchButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						search();
					}
				});

		findViewById(R.id.searchScreen_cancelButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						onBackPressed();
					}
				});
	}
	
	//ASSUME ALL FIELDS ARE INPUTTED
	public void search(){
		searchedItems = null;
		searchedItems = new ArrayList<Item>();
		
		for (Item i: LoginActivity.ItemList){
			if(entry_itemName.getText().toString().equals(i.itemName) | 
			   entry_address.getText().toString().equals(i.streetAddress) |
			   entry_country.getText().toString().equals(i.country) |
			   String.valueOf(spinner_category.getSelectedItem()).equals(i.category))				
				searchedItems.add(i);
		}
		
		ItemListViewActivity.list = searchedItems;
		ItemListViewActivity.isSearched = true;
		Intent itemListViewActivity = new Intent(
				"android.intent.action.ITEMLISTVIEWACTIVITY");
		startActivity(itemListViewActivity);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Intent regHomeActivity = new Intent(
				"android.intent.action.REGHOMEACTIVITY");
		startActivity(regHomeActivity);
	}
	
	/**
	 * adds categories to the spinner for categories
	 */
	public void addCategoriesToSpinner() {

		ArrayList<String> list = new ArrayList<String>();
		list.add("Keep Sake");
		list.add("Miscellaneous");
		list.add("Heirloom");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_category.setAdapter(dataAdapter);
	}
}
